package com.gsmr.controller;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.gsmr.entity.PageBean;
import com.gsmr.entity.ServerState;
import com.gsmr.entity.Serverinfo;
import com.gsmr.service.ServerStateService;
import com.gsmr.util.PropertityUtil;
import com.gsmr.util.ResponseUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
* @author 作者 : 黄志新
* @version 创建时间：2018年5月24日 上午10:46:55
* 类说明：
*/
@Controller
@RequestMapping("/serverState")
public class ServerStateController {
	
	@Resource
	private ServerStateService serverStateService;
	
	
	/**
	 * 分页条件查询ServerState
	 */
	@RequestMapping("/list")
	public String list(
			@RequestParam(value="page",required=false)String page,
			@RequestParam(value="rows",required=false)String rows,
			ServerState serverState,HttpServletResponse response)throws Exception{
		
		PageBean pageBean = new PageBean(Integer.parseInt(page), Integer.parseInt(rows));
		Map<String, Object> map = new HashMap<>();
		map.put("start", pageBean.getStart());
		map.put("size", pageBean.getPageSize());
		map.put("database_name", PropertityUtil.NETWORK_MANAGEMENT_DATABASE_NAME);
		System.out.println(map);
		List<ServerState> frontprocessorInfoList = serverStateService.find(map);
		System.out.println(frontprocessorInfoList.size());
		for (ServerState state : frontprocessorInfoList) {
			Map<String, Object>newMap = new HashMap<>();
			newMap.put("serverId", state.getServerId());
			List<Serverinfo>serverinfos = serverStateService.getServer(newMap);
			if (serverinfos == null || serverinfos.size() < 1) continue;
			state.setServerName(serverinfos.get(0).getServerName() + "空口服务器");
		}
		System.out.println(frontprocessorInfoList);
		Long total = serverStateService.getTotal(map);
		JSONObject result = new JSONObject();
		JSONArray jsonArray = JSONArray.fromObject(frontprocessorInfoList);
		result.put("rows", jsonArray);
		result.put("total", total);
		ResponseUtil.write(response, result);
		return null;
	}
	
	/**
	 * 查询ServerState的实时CPU使用率、内存使用率、硬盘使用率
	 */
	@RequestMapping("/listRealtimeUsageRate")
	public String listRealtimeUsageRate(
			HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("start", 0);
		map.put("size", 1);
		map.put("database_name", PropertityUtil.NETWORK_MANAGEMENT_DATABASE_NAME);
		List<ServerState> tmpList = serverStateService.find(map);
		JSONObject result = new JSONObject();
		JSONArray jsonArray = JSONArray.fromObject(tmpList);
		result.put("rows", jsonArray);
		ResponseUtil.write(response, result);
		return null;
	}
	
	/**
	 * 查询ServerState的历史CPU使用率、内存使用率、硬盘使用率
	 * 
	 * @param frontProcessorId
	 * @param startTime
	 * @param endTime
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/listHistoryUsageRate")
	public String listHistoryUsageRate(
			@RequestParam(value="startTime",required=false)String startTime,
			@RequestParam(value="endTime",required=false)String endTime,
			HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("database_name", PropertityUtil.NETWORK_MANAGEMENT_DATABASE_NAME);
		if(startTime!=null&&!startTime.equals("")&&!startTime.equals("NaN")){
			map.put("startTime", startTime);
		}
		if(endTime!=null&&!endTime.equals("")&&!endTime.equals("NaN")){
			map.put("endTime", endTime);
		}
		List<ServerState> tmpList = serverStateService.find(map);
		JSONObject result = new JSONObject();
		JSONArray jsonArray = JSONArray.fromObject(tmpList);
		result.put("rows", jsonArray);
		ResponseUtil.write(response, result);
		return null;
	}
	
	@Value("${jdbc.server1.servername}")
	private String serverName1;
	@Value("${jdbc.server1.online}")
	private Integer serverOnline1;
	@Value("${jdbc.mysql1.url}")
	private String url1;

	@Value("${jdbc.server2.servername}")
	private String serverName2;
	@Value("${jdbc.server2.online}")
	private Integer serverOnline2;
	@Value("${jdbc.mysql2.url}")
	private String url2;

	@Value("${jdbc.server3.servername}")
	private String serverName3;
	@Value("${jdbc.server3.online}")
	private Integer serverOnline3;
	@Value("${jdbc.mysql3.url}")
	private String url3;

	@Value("${jdbc.server4.servername}")
	private String serverName4;
	@Value("${jdbc.server4.online}")
	private Integer serverOnline4;
	@Value("${jdbc.mysql4.url}")
	private String url4;

	@Value("${jdbc.server5.servername}")
	private String serverName5;
	@Value("${jdbc.server5.online}")
	private Integer serverOnline5;
	@Value("${jdbc.mysql5.url}")
	private String url5;

	@Value("${jdbc.server6.servername}")
	private String serverName6;
	@Value("${jdbc.server6.online}")
	private Integer serverOnline6;
	@Value("${jdbc.mysql6.url}")
	private String url6;

	@Value("${jdbc.server7.servername}")
	private String serverName7;
	@Value("${jdbc.server7.online}")
	private Integer serverOnline7;
	@Value("${jdbc.mysql7.url}")
	private String url7;

	@Value("${jdbc.server8.servername}")
	private String serverName8;
	@Value("${jdbc.server8.online}")
	private Integer serverOnline8;
	@Value("${jdbc.mysql8.url}")
	private String url8;

	@Value("${jdbc.server9.servername}")
	private String serverName9;
	@Value("${jdbc.server9.online}")
	private Integer serverOnline9;
	@Value("${jdbc.mysql9.url}")
	private String url9;

	@Value("${jdbc.server10.servername}")
	private String serverName10;
	@Value("${jdbc.server10.online}")
	private Integer serverOnline10;
	@Value("${jdbc.mysql10.url}")
	private String url10;

	@Value("${jdbc.server11.servername}")
	private String serverName11;
	@Value("${jdbc.server11.online}")
	private Integer serverOnline11;
	@Value("${jdbc.mysql11.url}")
	private String url11;

	@Value("${jdbc.server12.servername}")
	private String serverName12;
	@Value("${jdbc.server12.online}")
	private Integer serverOnline12;
	@Value("${jdbc.mysql12.url}")
	private String url12;

	@Value("${jdbc.server13.servername}")
	private String serverName13;
	@Value("${jdbc.server13.online}")
	private Integer serverOnline13;
	@Value("${jdbc.mysql13.url}")
	private String url13;

	@Value("${jdbc.server14.servername}")
	private String serverName14;
	@Value("${jdbc.server14.online}")
	private Integer serverOnline14;
	@Value("${jdbc.mysql14.url}")
	private String url14;

	@Value("${jdbc.server15.servername}")
	private String serverName15;
	@Value("${jdbc.server15.online}")
	private Integer serverOnline15;
	@Value("${jdbc.mysql15.url}")
	private String url15;

	@Value("${jdbc.server16.servername}")
	private String serverName16;
	@Value("${jdbc.server16.online}")
	private Integer serverOnline16;
	@Value("${jdbc.mysql16.url}")
	private String url16;

	@Value("${jdbc.server17.servername}")
	private String serverName17;
	@Value("${jdbc.server17.online}")
	private Integer serverOnline17;
	@Value("${jdbc.mysql17.url}")
	private String url17;

	@Value("${jdbc.server18.servername}")
	private String serverName18;
	@Value("${jdbc.server18.online}")
	private Integer serverOnline18;
	@Value("${jdbc.mysql18.url}")
	private String url18;
	
	@RequestMapping("/getServerId")
	public void getServerId(HttpServletResponse response) throws Exception {
//		Map<String, Object> map = new HashMap<>();
//		
//		map.put("database_name", PropertityUtil.NETWORK_MANAGEMENT_DATABASE_NAME);
//		List<Serverinfo> serverId = serverStateService.getServer(map);
//		System.out.println(serverName1);
//		System.out.println(serverOnline1);
//		System.out.println(serverName2);
//		System.out.println(serverOnline2);
		
		JSONArray jsonArray = new JSONArray();
		
		for (int i = 1; i <= 18; i++) {
			//反射获取controller页面已定义的服务器信息
			Field serverName = this.getClass().getDeclaredField("serverName" + i);
			Field serverOnline = this.getClass().getDeclaredField("serverOnline" + i);
			serverName.setAccessible(true);
			serverOnline.setAccessible(true);
			
			String name = (String) serverName.get(this);
			Integer online = (Integer) serverOnline.get(this);
			if (online != 1) continue;
			Field url = this.getClass().getDeclaredField("url" + i);
			url.setAccessible(true);
			String u = (String) url.get(this);
			
			JSONObject jsonObject = new JSONObject();
			jsonObject.put("name", name);
			jsonObject.put("id", "dataSource" + i);
			jsonObject.put("url", u.split(":")[2]);
			jsonArray.add(jsonObject);
//			System.out.println(name);
//			System.out.println(online);
		}
		
//		for(int i = 0; i < serverId.size(); i++) {
//			Serverinfo serverInfo = serverId.get(i);
//			if (serverInfo.getOnline() != 1) continue;
//			JSONObject jsonObject = new JSONObject();
//			jsonObject.put("name", serverInfo.getServerName());
//			//System.out.println(serverId.get(i));
//			jsonObject.put("id", "dataSource" + (i + 1));
//			jsonArray.add(jsonObject);
//		}
		if (jsonArray.size() > 0) {
			jsonArray.getJSONObject(0).put("selected", true);
		}
		ResponseUtil.write(response, jsonArray);
	}
}